package com.byedu.course;

import java.lang.reflect.InvocationTargetException;
import java.util.Date;

import com.byedu.domain.course.vo.WeiXinUserVO;
import com.byedu.domain.course.UserDO;
import com.byedu.domain.course.WeiXinUserDO;
import com.byedu.util.IDGeneratorUtil;
import com.byedu.web.handler.ExceptionHandler;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.byedu.base.BaseService;
import com.byedu.mapper.course.UserMapper;
import com.byedu.mapper.course.WeiXinUserMapper;

/**
 * Created by jason on 2017/5/14.
 */
@Service
public class WeiXinUserService extends BaseService {
    private static final Log LOGGER = LogFactory.getLog(WeiXinUserService.class);
    @Autowired
    WeiXinUserMapper weiXinUserMapper;
    @Autowired
    UserMapper userMapper;

    /**
     * @param weiXinUserDO
     * @return
     * @description 微信授权登陆
     */
    @Transactional
    public WeiXinUserDO loginByWeixin(WeiXinUserDO weiXinUserDO) {
        if (weiXinUserDO != null && !StringUtils.isEmpty(weiXinUserDO.getOpenId())) {
            java.text.DateFormat format1 = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String date = format1.format(new Date());
            try {

                WeiXinUserDO olderweiXinUserDO = weiXinUserMapper.getWeiXinUserByOpenId(weiXinUserDO.getOpenId());
                if (olderweiXinUserDO != null) {
                    weiXinUserDO.setNewFlag(false);
                    weiXinUserDO.setLoginTime(date);
                    // 更新用户信息 用户登录时间是有数据库自动更新
                    LOGGER.info("更新用户信息 用户登录时间是有数据库自动更新");
                    weiXinUserMapper.updateWeiXinUserbyOpenId(weiXinUserDO);

                } else {
                    // 用户之前未使用过微信登录
                    LOGGER.info("用户之前未使用过微信登录");
                    // 1.先在bay_user表生成一条记录
                    UserDO userDO = new UserDO();
                    userDO.setId(IDGeneratorUtil.generateKeyByUUID());
                    userMapper.insertUser(userDO);
                    // 2.然后将用户信息存储到bay_wx_user表
                    weiXinUserDO.setUserId(userDO.getId());
                    weiXinUserDO.setLoginTime(date);
                    weiXinUserDO.setCreatedTime(date);
                    weiXinUserDO.setNewFlag(true);
                    weiXinUserMapper.saveWeiXinUser(weiXinUserDO);
                }
                //获取最新的微信用户信息
            } catch (Exception e) {
                logger.error("存储微信用户信息出错 ecountering error", e);
            }
        }

        return weiXinUserDO;
    }

    /**
     * 用于更新用户的手机号，验证码
     * @param userId
     * @param code
     * @param phone
     * @return
     */
    public int updateWeiXinUserbyUserId(String userId,String code,String phone){

            WeiXinUserDO weiXinUserDO = new WeiXinUserDO();
            weiXinUserDO.setUserId(userId);
            weiXinUserDO.setIdentifyCode(code);
            weiXinUserDO.setCellPhone(phone);
            return weiXinUserMapper.updateWeiXinUserbyUserId(weiXinUserDO);
        }

    public WeiXinUserVO getWeiXinUserVOByUserId(String userId) {

        WeiXinUserDO weiXinUserDO = weiXinUserMapper.getWeiXinUserByUserId(userId);

        WeiXinUserVO weiXinUserVO = new WeiXinUserVO();
        try {
            BeanUtils.copyProperties(weiXinUserVO, weiXinUserDO);
        } catch (Exception e) {

            logger.error("Do转化为Vo报错", e);
        }
        return weiXinUserVO;
    }


    public WeiXinUserVO getWeiXinUserVOByOpenId(String openId) {

        WeiXinUserDO weiXinUserDO = weiXinUserMapper.getWeiXinUserByOpenId(openId);

        WeiXinUserVO weiXinUserVO = new WeiXinUserVO();
        if(weiXinUserDO!=null) {
            try {
                BeanUtils.copyProperties(weiXinUserVO, weiXinUserDO);
            } catch (Exception e) {

                logger.error("Do转化为Vo报错", e);
            }
        }
        return weiXinUserVO;
    }


}
